/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

/**
 *
 * @author Barbar
 */
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

class transferFileServer extends Thread {

    Socket ClientSoc;
    DataInputStream din;
    DataOutputStream dout;
    String Command = null;

    transferFileServer(Socket soc) {
        try {
            ClientSoc = soc;
            ClientSoc.setSoTimeout(10000);
            din = new DataInputStream(ClientSoc.getInputStream());
            dout = new DataOutputStream(ClientSoc.getOutputStream());
            System.out.println("Klient FTP połączony");
            start();

        } catch (Exception ex) {
        }
    }

    public void stopTransfer(int time) {
        long t0, t1;
        t0 = System.currentTimeMillis();
        do {
            t1 = System.currentTimeMillis();
        } while (t1 - t0 < time);
        if (t1 - t0 == time && Command.equals(null) && din == null) {
            Command = "DISCONNECT";
            this.interrupt();
        }
    }

    void SendFile() throws Exception {
        if (!ClientSoc.isClosed()) {
            String filename = din.readUTF();
            File f = new File(filename);
            if (!f.exists()) {
                dout.writeUTF("File Not Found");
                return;
            } else {
                dout.writeUTF("READY");
                FileInputStream fin = new FileInputStream(f);
                int ch;
                do {
                    ch = fin.read();
                    dout.writeUTF(String.valueOf(ch));
                } while (ch != -1);
                fin.close();
                dout.writeUTF("File Receive Successfully");

                return;


            }
        } else {
            System.out.println("Zamknięto socket klienta - brak mozliwosci wysylu danych");
            return;
        }
    }

    void ReceiveFile() throws Exception {
        if (!ClientSoc.isClosed()) {
            String filename = din.readUTF();
            if (filename.compareTo("File not found") == 0) {
                return;
            }
            File f = new File(filename);


            FileOutputStream fout = new FileOutputStream(f);
            int ch;
            String temp;
            dout.writeUTF("AAAAAAAAAAAAAAA");
            do {
                temp = din.readUTF();
                ch = Integer.parseInt(temp);
                if (ch != -1) {
                    fout.write(ch);
                }
            } while (ch != -1);
            fout.close();
            dout.writeUTF("Wysłano plik pomyślnie");
            return;

        } else {
            System.out.println("Zamknięto socket klienta - brak mozliwosci wysylu danych");
            return;
        }
    }

    public void run() {

        try {

            System.out.println("Oczekiwanie na komendy ...");

            try {
                Command = din.readUTF();
                if (Command.compareTo("GET") == 0) {
                    System.out.println("Otrzymano polecenie GET ...");
                    SendFile();
                    return;

                } else if (Command.compareTo("PUT") == 0) {
                    System.out.println("Otrzymano polecenie PUT ...");
                    ReceiveFile();
                    return;

                } 
            } catch (IOException ex) {
                Logger.getLogger(transferFileServer.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (Exception ex) {
        }
    }
}